package com.example.banksys;

import DataBase.myDB;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;

import java.sql.ResultSet;
import java.sql.SQLException;

public class SubAccountDetailController {
    public TableColumn<subAccount,String> sub_type;
    public TableColumn<subAccount,Number> sub_balance;
    public TableColumn<subAccount,String> start_date;
    public TableColumn<subAccount,String> latest_date;
    public TableView<subAccount> sub_account_table;

    protected ObservableList<subAccount> subAccountObservableList;

    private long account_ID;

    public long getAccount_ID() {
        return account_ID;
    }

    public void setAccount_ID(long account_ID) {
        this.account_ID = account_ID;
    }

    public void initialize() throws SQLException {
        subAccountObservableList= FXCollections.observableArrayList();
        String sql="select * from sub_account natural join trade_type where account_ID="+QueryPageController.acc_ID;
        myDB sub_DB=new myDB("banksystem");
        ResultSet res=sub_DB.getStatement().executeQuery(sql);
        while(res.next()){
            subAccount surr=new subAccount();
            surr.setType_ID(res.getLong("type_ID"));
            surr.setType_name(res.getString("type_name"));
            surr.setSub_balance(res.getDouble("sub_balance"));
            surr.setStart_date(res.getTimestamp("start_date").toString());
            surr.setLatest_update(res.getTimestamp("latest_update").toString());
            subAccountObservableList.add(surr);
        }
        sub_account_table.setItems(subAccountObservableList);
        sub_type.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<subAccount, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<subAccount, String> subAccountStringCellDataFeatures) {
                return new SimpleStringProperty(subAccountStringCellDataFeatures.getValue().getType_name());
            }
        });
        sub_balance.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<subAccount, Number>, ObservableValue<Number>>() {
            @Override
            public ObservableValue<Number> call(TableColumn.CellDataFeatures<subAccount, Number> subAccountNumberCellDataFeatures) {
                return new SimpleDoubleProperty(subAccountNumberCellDataFeatures.getValue().getSub_balance());
            }
        });
        start_date.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<subAccount, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<subAccount, String> subAccountStringCellDataFeatures) {
                return new SimpleStringProperty(subAccountStringCellDataFeatures.getValue().getStart_date());
            }
        });
        latest_date.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<subAccount, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<subAccount, String> subAccountStringCellDataFeatures) {
                return new SimpleStringProperty(subAccountStringCellDataFeatures.getValue().getLatest_update());
            }
        });
    }
}
